include('shared.lua')


local matLight 		= Material( "sprites/light_ignorez" )
//local matSpark      = Material( "effects/spark" )


function ENT:FakeInit()
    self:SetSkin( 3 )

    self.NextSearch = CurTime()
    
    self.PixVis = util.GetPixelVisibleHandle()
    
    self.LightAttachment = self:LookupAttachment("light") 
    self:LoadSettings()
	
	self.TimeOffset = CurTime() % 6
end



function ENT:DrawSprite( LightPos, r, g, b )
    local Visibile	= util.PixelVisible( LightPos, 4, self.PixVis )
    
    if Visibile != nil then
	
		render.DrawSprite( LightPos , 8, 8, Color(r,g,b, 255 * Visibile) )
		
	end
end


function ENT:Draw()
	self:DrawModel()
	
	
	
    render.SetMaterial( matLight )
    
    
    local BonePos = self:GetBonePosition( self.EyeBonePos )
    local LightAttch = self:GetAttachment( self.LightAttachment ).Pos
    
    if OnBuild() then  //On build, stay put.
        
        self:SetPitch( 0 )
        self:SetYaw  ( 0 )
  
        self:DrawSprite( LightAttch, 255, 255, 0 )   
        
    elseif self:GetNWBool("Vtrg") then //When firing, aim at the target

        local TargetPly = self:GetNWEntity("trg")
        local GoAng = (TargetPly:EyePos() - BonePos):Normalize():Angle() - self:GetAngles()
        
        //fix lua for going between 0 and 360, instead of -180 and 180
        if GoAng.p > 180 then GoAng.p = GoAng.p - 360 end 
        if GoAng.y > 180 then GoAng.y = GoAng.y - 360 end
        
        self:SetPitch( GoAng.p )
        self:SetYaw  ( GoAng.y )

        self:DrawSprite( LightAttch, 0, 255, 0 )
        
    else //Keep looking around
        
        self:SetPitch( 0 )
        self:SetYaw  ( math.sin( CurTime() + self.TimeOffset ) * 60 )
  
        self:DrawSprite( LightAttch, 255, 0, 0 )   
    end
    
	
	
	/*
	local BonePos = self:GetBonePosition( 2 ) 
	
	
	render.SetMaterial( Material( "effects/spark" ) )
	
	for _, v in pairs( player.GetAll() ) do
            
        if v:Alive() && self:InConeView( BonePos, v ) then //&& self:GetTeam() != v:Team()  then
                
            local GoAng = (v:GetPos() - BonePos):Normalize()
            local fDelta = 1 ^ 0.5
            local sinWave = math.sin( fDelta * math.pi )
            
            //Msg( tostring(GoAng) .. "  ")
            //Msg( tostring(GoAng:Angle() ) .. "\n")
            
            render.DrawBeam(  BonePos,
					 BonePos + GoAng * 128,
					 2 + sinWave * 16,					
					 1,					
					 0,				
		  		      Color( 0, 255, 0, 255 ) )
            
        end
        
    end
    */
	
end




function ENT:Think()    

end
